package com.huangdada.hfweather.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huangdada.hfweather.bean.SJCity;
import com.huangdada.hfweather.mapper.CityMapper;
import com.huangdada.hfweather.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author huanglibin
 * @Project json
 * @Description JSON解析
 * @Date 2020-05-09-00-24-11
 * @Version 1.0
 */
@Component
public class JsonLib {

    private static CityService cityServices;

    @Autowired
    public JsonLib(CityService cityService){
        JsonLib.cityServices = cityService;
    }

//    public static void main(String[] args) {
//        String jsonArrayStr = readFileDto();
//        List<SJCity> cityList = testJSONStrToJSONArray(jsonArrayStr);
//        cityList.forEach(city -> {
//            cityServices.addSJCityInfo(city);
//        });
//    }

    public void test() {
        String jsonArrayStr = readFileDto();
        List<SJCity> cityList = testJSONStrToJSONArray(jsonArrayStr);
        cityList.forEach(city -> {
            cityServices.addSJCityInfo(city);
        });
    }

    /**
     * JSON数组转JSON对象
     *
     * @param jsonArrayStr
     */
    public static List<SJCity> testJSONStrToJSONArray(String jsonArrayStr) {
//        List<SJCity> cityList = new ArrayList<>();
//        SJCity city = null;
//        JSONArray jsonArray = JSON.parseArray(jsonArrayStr);
//        //循环遍历
//        int size = jsonArray.size();
//        for (int i = 0; i < size; i++) {
//            try {
//                JSONObject jsonObject = jsonArray.getJSONObject(i);
//                city = new SJCity();
//                city.setId(Integer.valueOf(jsonObject.getString("id")));
//                city.setPid(Integer.valueOf(jsonObject.getString("pid")));
//                city.setAreaid(jsonObject.getString("city_code"));
//                city.setAreaCode(jsonObject.getString("area_code"));
//                city.setCitycn(jsonObject.getString("city_name"));
//                city.setPostCode(jsonObject.getString("post_code"));
//                city.setCtime(parse(jsonObject.getString("ctime")));
//                cityList.add(city);
//            } catch (Exception e) {
//                System.out.println(e.getMessage());
//            }
//        }
//        if (cityList.size() > 0) {
//            cityList.forEach(city1 -> {
////                System.out.println(city1.toString());
//            });
//            return cityList;
//        }
        return null;
    }

    /**
     * 读文件
     *
     * @return
     */
    public static String readFileDto() {
        File file = new File("D:\\city.txt");
        if (!file.exists()) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static String formatDate(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date parse(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(strDate);
    }
}
